package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Order;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter {

	private Context context;
	private List<Order> data;
	private Handler mHandler;

	public OrderAdapter(Context context) {
		this.context = context;
	}

	public OrderAdapter(Context context, List<Order> data) {
		this.data = data;
		this.context = context;
	}

	public void setHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}

	public void setData(List<Order> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void clear() {
		this.data.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<Order> data) {
		if (data != null) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_order,
					null);
			holdView = new HoldView();
			holdView.orderNumTv = (TextView) view.findViewById(R.id.order_num);
			holdView.orderTimeTv = (TextView) view
					.findViewById(R.id.order_time);
			holdView.orderStatusTv = (TextView) view
					.findViewById(R.id.order_status);
			holdView.mOptionB = (Button) view.findViewById(R.id.order_option);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		Order o = data.get(position);
		holdView.orderNumTv.setText("订单" + o.getId());
		holdView.orderTimeTv.setText(o.getInstallTime());
		switch (o.getStatus()) {
		case 1: // 已完成
			holdView.orderStatusTv.setText(context
					.getString(R.string.order_status1));
			holdView.mOptionB.setText(context.getString(R.string.after_sales));
			holdView.mOptionB.setEnabled(true);
			break;
		case 2: // 已受理
			holdView.orderStatusTv.setText(context
					.getString(R.string.order_status2));
			holdView.mOptionB.setText(context.getString(R.string.cancel_order));
			holdView.mOptionB.setEnabled(true);
			break;
		case 8: // 未受理
			holdView.orderStatusTv.setText(context
					.getString(R.string.order_status4));
			holdView.mOptionB.setText(context.getString(R.string.cancel_order));
			holdView.mOptionB.setEnabled(true);
			break;
		case 9: // 已撤销
			holdView.orderStatusTv.setText(context
					.getString(R.string.order_status3));
			holdView.mOptionB.setText("");
			holdView.mOptionB.setEnabled(false);
			break;
		default:
			break;
		}
		holdView.mOptionB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Message msg = Message.obtain();
				msg.what = 1;
				msg.arg1 = position;
				mHandler.sendMessage(msg);
			}
		});

		return view;
	}

	HoldView holdView;

	class HoldView {
		private TextView orderNumTv, orderTimeTv, orderStatusTv;
		private Button mOptionB;
	}

}
